from jinja2 import Environment, PackageLoader
import json
import os

loader = PackageLoader("axios", "templates")
enviroment = Environment(loader=loader)
tpl = enviroment.get_template("get.tpl")

path = os.path.join(os.path.dirname(__file__), "swagger.json")

data = {}
with open(path, "rb") as swaggerjson:
    data = json.load(swaggerjson)
result = ""
for path, info in data.items():
    render_data = {"url": path}
    for method, methodInfo in info.items():
        render_data["method"] = method
        render_data["label"] = methodInfo.get("summary")
        render_data["params"] = methodInfo.get("parameters")
        operationId = methodInfo.get("operationId")
        apiName = "".join(operationId.split("-"))
        render_data["apiName"] = apiName
        result += "\n\n"
        result += tpl.render(data=render_data)

result_path = os.path.join(os.path.dirname(__file__), "result")

file_path = os.path.join(result_path, "request.ts")

with open(file_path, "wb") as file:
    file.write(result.encode("utf-8"))
